import defaultIcon from '@/assets/images/file_default.webp';
import docIcon from '@/assets/images/file_doc.webp';
import pdfIcon from '@/assets/images/file_pdf.webp';
import pptIcon from '@/assets/images/file_ppt.webp';
import txtIcon from '@/assets/images/file_txt.webp';
import videoIcon from '@/assets/images/file_video.webp';
import xlsIcon from '@/assets/images/file_xls.webp';
import audioIcon from '@/assets/images/flie_audio.webp';

export const fileType = {
  default: defaultIcon,
  doc: docIcon,
  pdf: pdfIcon,
  ppt: pptIcon,
  txt: txtIcon,
  video: videoIcon,
  xls: xlsIcon,
  audio: audioIcon
};

const doc = ['doc', 'docx'];
const ppt = ['ppt', 'pptx', 'pptm'];
const excel = ['xls', 'xlsx'];
const video = ['mp4', 'avi', 'mov', 'wmv', 'flv', 'mkv', 'm4v', 'f4v'];
const audio = ['mp3', 'wav', 'aac', 'ogg'];
const txt = ['txt', 'log', 'md', 'ini', 'json', 'xml', 'yaml', 'yml'];
const pdf = ['pdf'];
const zip = ['zip', 'rar', '7z', 'tar'];

/**
 * 根据文件名或 url 获取文件扩展
 * @param file 文件名或 url
 * @returns
 */
export function getFileExtension(file: string) {
  if (!file) return '';
  const dot = file.lastIndexOf('.');
  if (dot == -1) {
    return fileType.default;
  }
  return file.slice(dot + 1).toLowerCase();
}

/**
 * 根据文件扩展获取文件图标
 * @param fileExtension  文件扩展信息
 * @returns
 */
export function getFileIconByExtension(fileExtension: string) {
  if (!fileExtension) {
    return fileType.default;
  }
  if (doc.includes(fileExtension)) {
    return fileType.doc;
  }
  if (ppt.includes(fileExtension)) {
    return fileType.ppt;
  }

  if (excel.includes(fileExtension)) {
    return fileType.xls;
  }

  if (video.includes(fileExtension)) {
    return fileType.video;
  }
  if (audio.includes(fileExtension)) {
    return fileType.audio;
  }
  if (txt.includes(fileExtension)) {
    return fileType.txt;
  }

  if (pdf.includes(fileExtension)) {
    return fileType.pdf;
  }

  if (zip.includes(fileExtension)) {
    return fileType.default;
  }

  return fileType.default;
}

/**
 * 根据路径获取 icon
 * @param url
 * @returns
 */
export function getIconByUrl(url: string) {
  const extension = getFileExtension(url);
  return getFileIconByExtension(extension);
}
/**
 * 根据文件类型获取文件图标
 * @param fileType 接口返回的文件类型，有可能匹配不到，就根据文件后缀去判断
 * @param url  文件地址或文件名
 */
export function getIconByFileTypeOrUrl(filetype: string, url: string) {
  const extension = getFileExtension(url);
  if (ppt.includes(filetype) || ppt.includes(extension)) {
    return fileType.ppt;
  }
  if (excel.includes(filetype) || excel.includes(extension)) {
    return fileType.xls;
  }

  if (video.includes(filetype) || video.includes(extension)) {
    return fileType.video;
  }
  if (audio.includes(filetype) || audio.includes(extension)) {
    return fileType.audio;
  }
  if (txt.includes(filetype) || txt.includes(extension)) {
    return fileType.txt;
  }

  if (pdf.includes(filetype) || pdf.includes(extension)) {
    return fileType.pdf;
  }

  if (zip.includes(filetype) || zip.includes(extension)) {
    return fileType.default;
  }
  return fileType.default;
}
